package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.util.ConverterUtils;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
* 网关流控 从nacos中读取规则
* @author cjunxian
* @date 2021/6/25
*/
@Component("GatewayFlowRuleNacosProvider")
public class GatewayFlowRuleNacosProvider implements DynamicRuleProvider<List<GatewayFlowRuleEntity>> {

    @Autowired
    private ConfigService configService;

    @Autowired
    ConverterUtils converterUtils;

    /**
     * 参考 flowrule provider部分
     * @param appName
     * @return
     * @throws Exception
     */
    @Override
    public List<GatewayFlowRuleEntity> getRules(String appName) throws Exception {

        return converterUtils.pullRule(configService
                ,appName
                ,NacosConfigUtil.GATEWAY_FLOW_DATA_ID_POSTFIX
                ,NacosConfigUtil.GROUP_ID
                ,3000
                ,GatewayFlowRuleEntity.class);
    }
}
